package com.sl.core.engine.meta.valuemeta;

import cn.hutool.core.util.StrUtil;
import org.apache.hop.core.row.IValueMeta;

import java.util.List;

/**
 * 对象或数组,子元素接口
 *
 */
public interface ObjArrayMetaInterface extends IValueMeta {
    List<IValueMeta> getFields();

    void setFields(List<IValueMeta> childMetaList);

    void addField(IValueMeta childMete);

    void addFields(List<IValueMeta> childMete);

    IValueMeta getField(String fieldName);

    default void remove(String name) {
        Integer index = indexOfValue(name);
        if (index < 0) {
            return;
        }
        List<IValueMeta> fields = getFields();
        fields.remove(index.intValue());
    }

    default IValueMeta searchValueMeta(String name) {
        Integer index = indexOfValue(name);
        if (index < 0) {
            return null;
        }
        return getFields().get(index);
    }

    default Integer indexOfValue(String name) {
        for (int i = 0; i < getFields().size(); i++) {
            IValueMeta valueMetaInterface = getFields().get(i);
            if (StrUtil.equals(valueMetaInterface.getName(), name)) {
                return i;
            }
        }
        return -1;
    }
}
